//
//  RHMePolicyAddTipsView.m
//  RuiHeApp
//
//  Created by Yan Chen on 2025/1/21.
//

#import "RHMePolicyAddTipsView.h"
@interface RHMePolicyAddTipsView ()
@property (nonatomic, strong) QMUILabel *titleLab;

@property (nonatomic ,strong) QMUILabel *detaiLab;

@property (nonatomic, strong) QMUITextField *textField;
@end
@implementation RHMePolicyAddTipsView

-(instancetype)init
{
    if (self = [super init]) {
        [self setupUI];
    }
    return self;
}

-(void)showWithTitle:(NSString*)title detail:(NSString*)detail;
{
    [kApplication.keyWindow addSubview:self];
    self.frame = kAppWindow.bounds;
    self.titleLab.text = title;
    self.detaiLab.text = detail;
}

-(void)setupUI
{
    
    self.backgroundColor = kCOLOR_HEX_A(@"000000", 0.4);
    UIView *bgView = kView;
    bgView.userInteractionEnabled = YES;
    bgView.backgroundColor = UIColor.whiteColor;
    [bgView rounded:8];
    [self addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(35);
        make.right.mas_offset(-35);
        make.centerY.mas_offset(0);
      
    }];
    
   
    
    UIImageView *icon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"me_policy_name_icon"]];
    [bgView addSubview:icon];
    [icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(48);
        make.left.mas_offset(15);
        make.top.mas_offset(15);
    }];
    
    
    QMUIButton *closeBtn = [[QMUIButton alloc]init];
    [closeBtn setImage:[UIImage imageNamed:@"close_icon"] forState:UIControlStateNormal];
    [bgView addSubview:closeBtn];
    [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(40);
        make.top.mas_offset(10);
        make.right.mas_offset(-10);
        
    }];
    
    kWeakSelf;
    [[closeBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        [weakSelf tapAction];
    }];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)];
    [self addGestureRecognizer:tap];
    
    
    QMUILabel *patternTitleLab = [QMUILabel labelWithText:@"添加电价模板".rh_localized font:kBOLDSYSTEMFONT(18) textColor:kCOLOR_HEX(@"#101828")];
    patternTitleLab.numberOfLines = 0;
    patternTitleLab.textAlignment = NSTextAlignmentLeft;
    [bgView addSubview:patternTitleLab];
    self.titleLab = patternTitleLab;
    [patternTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.top.equalTo(icon.mas_bottom).offset(15);
        make.right.mas_offset(-15);
        
    }];
    
    QMUILabel *patternNameLab = [QMUILabel labelWithText:@"电价模板名称".rh_localized font:kSYSTEMFONT(15) textColor:kCOLOR_HEX(@"#474B53")];
    patternNameLab.numberOfLines = 0;
    patternNameLab.textAlignment = NSTextAlignmentLeft;
    [bgView addSubview:patternNameLab];
    self.detaiLab = patternNameLab;
    [patternNameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.top.equalTo(patternTitleLab.mas_bottom).offset(20);
        make.right.mas_offset(-15);
    }];
    
    QMUITextField *textField = [[QMUITextField alloc]init];
    textField.placeholder = @"请输入模板名称".rh_localized;
    textField.layer.borderColor = kCOLOR_HEX(@"#CDD0D6").CGColor;
    textField.layer.borderWidth = 1;
    textField.layer.cornerRadius = 8;
    textField.layer.masksToBounds = YES;
    textField.placeholderColor = kCOLOR_HEX(@"#A8ABB2");
    textField.font = kSYSTEMFONT(15);
    textField.textInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    [bgView addSubview:textField];
    self.textField = textField;
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.top.equalTo(patternNameLab.mas_bottom).offset(20);
        make.height.offset(45);
    }];
    
    QMUIButton *sureBtn = [QMUIButton buttonWithTitle:@"确认".rh_localized titleColor:UIColor.whiteColor titleFont:kBOLDSYSTEMFONT(16)];
    [sureBtn setBackgroundColor:CMainColorGreen];
    [sureBtn setCornerRadius:8];
    [bgView addSubview:sureBtn];
    [sureBtn addTarget:self action:@selector(sureAction) forControlEvents:UIControlEventTouchUpInside];
    [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.top.equalTo(textField.mas_bottom).offset(30);
        make.height.mas_offset(44);
    }];
    
    QMUIButton *cancelBtn = [QMUIButton buttonWithTitle:@"取消".rh_localized titleColor:UIColor.blackColor titleFont:kBOLDSYSTEMFONT(16)];
    [cancelBtn setBackgroundColor:UIColor.whiteColor];
    cancelBtn.layer.borderColor = kCOLOR_HEX(@"CDD0D6").CGColor;
    cancelBtn.layer.borderWidth = 1;
    cancelBtn.layer.masksToBounds = YES;
    [cancelBtn setCornerRadius:8];
    [bgView addSubview:cancelBtn];
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.top.equalTo(sureBtn.mas_bottom).offset(10);
        make.height.mas_offset(44);
        make.bottom.mas_offset(-15);
    }];
    [cancelBtn addTarget:self action:@selector(tapAction) forControlEvents:UIControlEventTouchUpInside];
}

-(void)tapAction
{
    [self removeFromSuperview];
}

-(void)sureAction
{
    if (self.textField.text.length>0 && self.finishBlock) {
        self.finishBlock(self.textField.text);
        [self removeFromSuperview];
    }
   
}

@end
